# -*-coding:utf-8 -*-
# Author : Mo
# Data : 2022/2/4 20:33
from django import forms

class BootStrap:
    # 重写name字段
    # name = forms.CharField(min_length=3,label="用户名")
    #
    # class Meta:
    #     model = models.UserInfo
    #     fields = ["name","password","age","account","create_time","gender","depart"]
    #     # widgets ={
    #     #     "name":forms.TextInput(attrs={"class":"form-control"}),
    #     #     "password": forms.PasswordInput(attrs={"class": "form-control"}),
    #     #     "age": forms.TextInput(attrs={"class": "form-control"}),
    #     #     "account": forms.DateInput(attrs={"class": "form-control"}),
    #     #     "create_time": forms.DateTimeInput(attrs={"class": "form-control"}),
    #     #     "gender":forms.Select(attrs={"class": "form-control"}),
    #     #     "depart": forms.Select(attrs={"class": "form-control"}),
    #     # }

    bootstrap_exclude_fields = []

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        # 循环找到所有的插件，添加class=form-control
        for name,field in self.fields.items():
            if name in self.bootstrap_exclude_fields:
                continue
            # 字段中有属性，保留原来的属性，没有属性，才增加。
            if field.widget.attrs:
                field.widget.attrs["class"] = "form-control"
                field.widget.attrs["placeholder"] = field.label
            else:
                field.widget.attrs = {
                    "class":"form-control",
                    "placeholder":field.label
                }


class BootStrapModelForm(BootStrap,forms.ModelForm):
    pass


class BootStrapForm(BootStrap,forms.Form):
    pass
